# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'editProduct.ui'
#
# Created: Fri Jun 01 01:53:15 2012
#      by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui
import pyodbc

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_Form(QtGui.QDialog):
    
    def getAllCategories(self,cur):
        self.categoryComboBox.clear()
        
        cur.execute("select * from Categories")
        rows = cur.fetchall()
        for row in rows:
            self.categoryComboBox.addItem(row.name)
            
    def getAllProducers(self,cur):
        self.producentComboBox.clear()
        
        cur.execute("select * from Producers")
        rows = cur.fetchall()
        for row in rows:
            self.producentComboBox.addItem(row.name)
            
    def getAllMeasures(self,cur):
        self.measureComboBox.clear()
        
        cur.execute("select * from Measures")
        rows = cur.fetchall()
        for row in rows:
            self.measureComboBox.addItem(row.name)
    
    def setAllComoboBoxes(self):
        conn = pyodbc.connect(self.databaseDir)
        cur = conn.cursor()
        self.getAllCategories(cur)
        self.getAllMeasures(cur)
        self.getAllProducers(cur)
        conn.close()
        
    def setProduct(self):
        '''
        Pobiera wszystkie produkty z bazy
        '''
        conn = pyodbc.connect(self.databaseDir)
        cur = conn.cursor()
        cur.execute("exec getProduct ?,?",self.name,self.producer)
        row = cur.fetchone()
        if row:
            self.nameLineEdit.setText(row[0])
            self.producentComboBox.setCurrentIndex(self.producentComboBox.findText(row[1]))
            self.categoryComboBox.setCurrentIndex(self.categoryComboBox.findText(row[2]))
            self.measureComboBox.setCurrentIndex(self.measureComboBox.findText(row[3]))
            self.priceLineEdit.setText(str(row[4]))
            self.quantityLineEdit.setText(str(row[5]))
        conn.close()
       
    
    def save(self):
        '''
        zapisuje zmiany
        '''
        conn = pyodbc.connect(self.databaseDir)
        cur = conn.cursor()
        
        cur.execute("exec getProductId ?,?",self.name,self.producer)
        row = cur.fetchone()
        if row:
            product_id = row[0]
            
        cur.execute("exec addName ?",str(self.nameLineEdit.text()))
        cur.execute("exec getName ?",str(self.nameLineEdit.text()))
        row = cur.fetchone()
        if row:
            id_name = row[0]
        cur.execute("exec changeName ?,?",product_id, id_name)
        conn.commit()
        
        cur.execute("exec getProducer ?",str(self.producentComboBox.currentText()))
        row = cur.fetchone()
        if row:
            id_producer = row[0]
        cur.execute("exec changeProducer ?,?",product_id,id_producer)
        conn.commit()
        
        cur.execute("exec getCategory ?",str(self.categoryComboBox.currentText()))
        row = cur.fetchone()
        if row:
            id_category = row[0]
        cur.execute("exec changeCategroy ?,?",product_id,id_category)
        conn.commit()
            
        cur.execute("exec getMeasure ?",str(self.measureComboBox.currentText()))
        row = cur.fetchone()
        if row:
            id_measure = row[0]
        cur.execute("exec changeMeasure ?,?",product_id,id_measure)
        conn.commit()
        
        cur.execute("exec changeFixed_quantity ?,?",product_id,float(self.quantityLineEdit.text()))
        cur.execute("exec changeFixed_price ?,?",product_id,float(self.priceLineEdit.text()))
        conn.commit()
        conn.close()
        self.ProductManagment.getAllProducts()
        self.close()
        
    
    def cancel(self):
        self.close()
        
        
    def setupUi(self, Form,name,producer,ProductManagment,databaseDir):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(400, 218)
        self.ProductManagment = ProductManagment
        self.name = name
        self.producer = producer
        self.databaseDir = databaseDir
        
        self.gridLayoutWidget = QtGui.QWidget(Form)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(20, 20, 371, 141))
        self.gridLayoutWidget.setObjectName(_fromUtf8("gridLayoutWidget"))
        self.addLayout = QtGui.QGridLayout(self.gridLayoutWidget)
        self.addLayout.setMargin(0)
        self.addLayout.setObjectName(_fromUtf8("addLayout"))
        self.nameLabel = QtGui.QLabel(self.gridLayoutWidget)
        self.nameLabel.setObjectName(_fromUtf8("nameLabel"))
        self.addLayout.addWidget(self.nameLabel, 0, 0, 1, 1)
        self.nameLineEdit = QtGui.QLineEdit(self.gridLayoutWidget)
        self.nameLineEdit.setObjectName(_fromUtf8("nameLineEdit"))
        self.addLayout.addWidget(self.nameLineEdit, 1, 0, 1, 1)
        self.quantityLabel = QtGui.QLabel(self.gridLayoutWidget)
        self.quantityLabel.setObjectName(_fromUtf8("quantityLabel"))
        self.addLayout.addWidget(self.quantityLabel, 0, 1, 1, 1)
        self.producerLabel = QtGui.QLabel(self.gridLayoutWidget)
        self.producerLabel.setObjectName(_fromUtf8("producerLabel"))
        self.addLayout.addWidget(self.producerLabel, 2, 0, 1, 1)
        self.producentComboBox = QtGui.QComboBox(self.gridLayoutWidget)
        self.producentComboBox.setObjectName(_fromUtf8("producentComboBox"))
        self.addLayout.addWidget(self.producentComboBox, 3, 0, 1, 1)
        self.categoryLabel = QtGui.QLabel(self.gridLayoutWidget)
        self.categoryLabel.setObjectName(_fromUtf8("categoryLabel"))
        self.addLayout.addWidget(self.categoryLabel, 4, 0, 1, 1)
        self.categoryComboBox = QtGui.QComboBox(self.gridLayoutWidget)
        self.categoryComboBox.setObjectName(_fromUtf8("categoryComboBox"))
        self.addLayout.addWidget(self.categoryComboBox, 5, 0, 1, 1)
        self.priceLabel = QtGui.QLabel(self.gridLayoutWidget)
        self.priceLabel.setObjectName(_fromUtf8("priceLabel"))
        self.addLayout.addWidget(self.priceLabel, 2, 1, 1, 1)
        self.quantityLineEdit = QtGui.QLineEdit(self.gridLayoutWidget)
        self.quantityLineEdit.setObjectName(_fromUtf8("quantityLineEdit"))
        self.addLayout.addWidget(self.quantityLineEdit, 1, 1, 1, 1)
        self.priceLineEdit = QtGui.QLineEdit(self.gridLayoutWidget)
        self.priceLineEdit.setObjectName(_fromUtf8("priceLineEdit"))
        self.addLayout.addWidget(self.priceLineEdit, 3, 1, 1, 1)
        self.measureComboBox = QtGui.QComboBox(self.gridLayoutWidget)
        self.measureComboBox.setObjectName(_fromUtf8("measureComboBox"))
        self.addLayout.addWidget(self.measureComboBox, 5, 1, 1, 1)
        self.measureLabel = QtGui.QLabel(self.gridLayoutWidget)
        self.measureLabel.setObjectName(_fromUtf8("measureLabel"))
        self.addLayout.addWidget(self.measureLabel, 4, 1, 1, 1)
        
        
        self.saveButton = QtGui.QPushButton(Form)
        self.saveButton.setGeometry(QtCore.QRect(220, 180, 81, 23))
        self.saveButton.setObjectName(_fromUtf8("saveButton"))
        self.saveButton.connect(self.saveButton, QtCore.SIGNAL("clicked()"),self.save)
        
        
        self.cancelButton = QtGui.QPushButton(Form)
        self.cancelButton.setGeometry(QtCore.QRect(310, 180, 75, 23))
        self.cancelButton.setObjectName(_fromUtf8("cancelButton"))
        self.cancelButton.connect(self.cancelButton, QtCore.SIGNAL("clicked()"),self.cancel)
        
        self.setAllComoboBoxes()
        self.setProduct()

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.nameLabel.setText(QtGui.QApplication.translate("Form", "Nazwa:", None, QtGui.QApplication.UnicodeUTF8))
        self.quantityLabel.setText(QtGui.QApplication.translate("Form", "Ilosc:", None, QtGui.QApplication.UnicodeUTF8))
        self.producerLabel.setText(QtGui.QApplication.translate("Form", "Producent:", None, QtGui.QApplication.UnicodeUTF8))
        self.categoryLabel.setText(QtGui.QApplication.translate("Form", "Kategoria:", None, QtGui.QApplication.UnicodeUTF8))
        self.priceLabel.setText(QtGui.QApplication.translate("Form", "Cena:", None, QtGui.QApplication.UnicodeUTF8))
        self.measureLabel.setText(QtGui.QApplication.translate("Form", "Jednostka:", None, QtGui.QApplication.UnicodeUTF8))
        self.saveButton.setText(QtGui.QApplication.translate("Form", "Zapisz", None, QtGui.QApplication.UnicodeUTF8))
        self.cancelButton.setText(QtGui.QApplication.translate("Form", "Anuluj", None, QtGui.QApplication.UnicodeUTF8))

